/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * XMLOpdracht2View.java
 *
 * Created on 8-okt-2009, 15:01:42
 */
package xmlopdracht2;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import java.util.Vector;
import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.plaf.basic.BasicMenuItemUI;
import xmlopdracht2.apachefop.PdfCreator;

/**
 *
 * @author Administrator
 */
public class XMLOpdracht2View extends javax.swing.JFrame implements MouseListener, ActionListener
{

  private Xml xml;
  private Vector studenten;
  private Integer selectedStudent;
  private JMenuItem menuItemOpen;
  private JMenuItem menuItemAfsluiten;

  /** Creates new form XMLOpdracht2View */
  public XMLOpdracht2View()
  {
    initComponents();
    this.xml = new Xml();
    this.setTitle("XML Opdracht 2 Groep 11");
    JMenuBar menuBar = new JMenuBar();

    JMenu menuItemAlgemeen = new JMenu("Algemeen");

    menuItemOpen = new JMenu("Open");
    menuItemOpen.addMouseListener(this);//addActionListener(this);
    menuItemOpen.setUI(new BasicMenuItemUI());

    menuItemAfsluiten = new JMenu("Afsluiten");
    menuItemAfsluiten.addMouseListener(this);
    menuItemAfsluiten.setUI(new BasicMenuItemUI());

    menuItemAlgemeen.add(menuItemOpen);
    menuItemAlgemeen.add(menuItemAfsluiten);
    menuBar.add(menuItemAlgemeen);
    this.setJMenuBar(menuBar);

    this.jButton1.setEnabled(false);
    this.jButton2.setEnabled(false);

    this.jButton1.addMouseListener(this);
    this.jButton2.addMouseListener(this);
    this.jButton3.addMouseListener(this);

    this.jList1.setListData(new Vector());
    this.jList1.addMouseListener(this);

    this.setSize(500, 500);
  }

  public Boolean changeStudent(Student student)
  {
    return xml.changeStudent(student, selectedStudent);
  }

  public Xml getXml()
  {
    return this.xml;
  }

  public Integer getSelectedStudent()
  {
    return this.selectedStudent;
  }

  public static void main(String args[])
  {
    java.awt.EventQueue.invokeLater(new Runnable()
    {

      public void run()
      {
        new XMLOpdracht2View().setVisible(true);
      }
    });
  }

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jScrollPane1 = new javax.swing.JScrollPane();
    jList1 = new javax.swing.JList();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();
    jButton3 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setName("Form"); // NOI18N

    jPanel1.setName("jPanel1"); // NOI18N

    jScrollPane1.setName("jScrollPane1"); // NOI18N

    jList1.setModel(new javax.swing.AbstractListModel() {
      String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
      public int getSize() { return strings.length; }
      public Object getElementAt(int i) { return strings[i]; }
    });
    jList1.setName("jList1"); // NOI18N
    jScrollPane1.setViewportView(jList1);

    jButton1.setLabel("Wijzig student"); // NOI18N
    jButton1.setName("jButton1"); // NOI18N

    jButton2.setLabel("Maak PDF"); // NOI18N
    jButton2.setName("jButton2"); // NOI18N

    jButton3.setText("Voeg student toe"); // NOI18N
    jButton3.setName("jButton3"); // NOI18N

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 557, Short.MAX_VALUE)
      .addGroup(jPanel1Layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(jButton1)
        .addGap(18, 18, 18)
        .addComponent(jButton2)
        .addGap(18, 18, 18)
        .addComponent(jButton3)
        .addContainerGap(292, Short.MAX_VALUE))
    );
    jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel1Layout.createSequentialGroup()
        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 363, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jButton1)
          .addComponent(jButton2)
          .addComponent(jButton3))
        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

  public void mouseClicked(MouseEvent e)
  {
    if (e.getSource() == this.menuItemAfsluiten)
    {
      this.dispose();
    }
    else if (e.getSource() == this.menuItemOpen)
    {
      JFileChooser fileChooser = new JFileChooser(new File("./"));
      fileChooser.setDialogTitle("Kies een XML bestand");
      fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
      fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
      fileChooser.addChoosableFileFilter(new SimpleFileFilter("XML - Bestanden", "*.xml", true));

      int returnValue = fileChooser.showOpenDialog(this);
      if (returnValue == JFileChooser.APPROVE_OPTION)
      {
        xml.setFile(fileChooser.getSelectedFile());
        if (!xml.validate())
        {
          JOptionPane.showMessageDialog(null, "Dit XML bestand is niet valid. Probeer een ander bestand", "Mislukt!", JOptionPane.INFORMATION_MESSAGE);
        }
        else
        {
          this.studenten = xml.getStudenten();
          this.jList1.setListData(new Vector());
          this.jList1.setListData(this.studenten);
        }
      }
    }
    //Wijzigen
    else if (e.getSource() == this.jButton1)
    {
      this.selectedStudent = this.jList1.getSelectedIndex();
      Student student = (Student) this.studenten.get(this.selectedStudent);
      StudentBox sb = new StudentBox(this, student);
      sb.setVisible(true);
    }
    //Maak PDF
    else if (e.getSource() == this.jButton2)
    {
      //Filename aan de user vragen
      JFileChooser fileChooser = new JFileChooser(new File("./"));
      fileChooser.setDialogTitle("Kies een PDF bestand");
      fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
      fileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
      fileChooser.addChoosableFileFilter(new SimpleFileFilter("PDF - Bestanden", "*.pdf", true));
      fileChooser.removeChoosableFileFilter(fileChooser.getAcceptAllFileFilter());
      int returnValue = fileChooser.showOpenDialog(this);

      if (returnValue == JFileChooser.APPROVE_OPTION)
      {
        try
        {
          //fileChooser.
          File saveFile = fileChooser.getSelectedFile();
          String fileName = saveFile.getName();
          if(!fileName.endsWith(".pdf"))
          {
            fileName += ".pdf";
          }
          PdfCreator.CreatePDF(
                  ((Student) xml.getStudenten().get(jList1.getSelectedIndex())).getStudentNr(),
                  xml.getFile(),
                  fileName);
        } catch (IOException ioEx)
        {
          JOptionPane.showMessageDialog(null,
                  "Er is een fout opgetreden bij het aanmaken van het pdfbestand\n" +
                  "Foutbericht: " + ioEx.getLocalizedMessage(),
                  "",
                  JOptionPane.ERROR_MESSAGE);
        }
      }
    }
    //Toevoegen
    else if (e.getSource() == this.jButton3)
    {
      StudentBox sb = new StudentBox(this);
      sb.setVisible(true);
    }
    else if (e.getSource() == this.jList1)
    {
      this.jButton1.setEnabled(true);
      this.jButton2.setEnabled(true);
    }
  }

  public void mousePressed(MouseEvent e)
  {
  }

  public void mouseReleased(MouseEvent e)
  {
  }

  public void mouseEntered(MouseEvent e)
  {
  }

  public void mouseExited(MouseEvent e)
  {
  }

  public void actionPerformed(ActionEvent e)
  {
  }
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton jButton1;
  private javax.swing.JButton jButton2;
  private javax.swing.JButton jButton3;
  private javax.swing.JList jList1;
  private javax.swing.JPanel jPanel1;
  private javax.swing.JScrollPane jScrollPane1;
  // End of variables declaration//GEN-END:variables
}
